/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 12/27/10
 * Time: 6:56 PM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.data {
import com.thoughtorigin.asset.CloningAssetCache;
import com.thoughtorigin.loader.AbstractLoader;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertTrue;
import org.mockito.integrations.between;
import org.mockito.integrations.given;
import org.mockito.integrations.mock;
import org.mockito.integrations.verify;

[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
[Mock(type="com.thoughtorigin.loader.AbstractLoader")]
public class SimpleDataCacheTest {

    private var _simpleDataCache: SimpleDataCache;

    [Before]
    public function setUp(): void {
        _simpleDataCache = new SimpleDataCache();
    }

    [After]
    public function tearDown(): void {
        _simpleDataCache = null;
    }

    [Test]
    public function shouldCacheData(): void {
        var loader: AbstractLoader = mock(AbstractLoader, "", ["url"]);
        given(loader.url).willReturn("url");
        given(loader.content).willReturn({});

        assertFalse(_simpleDataCache.isAssetCached("url"));
        _simpleDataCache.cacheAsset(loader);
        assertTrue(_simpleDataCache.isAssetCached("url"));
    }

    [Test]
    public function shouldOnlyCacheDataOnce(): void {
        var loader: AbstractLoader = mock(AbstractLoader, "", ["url"]);
        given(loader.url).willReturn("url");
        given(loader.content).willReturn({});

        assertFalse(_simpleDataCache.isAssetCached("url"));
        _simpleDataCache.cacheAsset(loader);
        _simpleDataCache.cacheAsset(loader);
        _simpleDataCache.cacheAsset(loader);
        assertTrue(_simpleDataCache.isAssetCached("url"));

        verify(between(1,1)).that(loader.content);
    }

    [Test]
    public function shouldDeleteCachedItem(): void {
        var loader: AbstractLoader = mock(AbstractLoader, "", ["url"]);
        given(loader.url).willReturn("url");
        given(loader.content).willReturn({});

        assertFalse(_simpleDataCache.isAssetCached("url"));
        _simpleDataCache.cacheAsset(loader);
        assertTrue(_simpleDataCache.isAssetCached("url"));

        _simpleDataCache.deleteAsset("url");
        assertFalse(_simpleDataCache.isAssetCached("url"));
    }

    [Test]
    public function shouldGetCachedObject(): void {
        var loader: AbstractLoader = mock(AbstractLoader, "", ["url"]);
        var obj: Object = {};
        given(loader.url).willReturn("url");
        given(loader.content).willReturn(obj);

        assertFalse(_simpleDataCache.isAssetCached("url"));
        _simpleDataCache.cacheAsset(loader);
        assertTrue(_simpleDataCache.isAssetCached("url"));

        assertEquals(obj, _simpleDataCache.getAsset("url"));
    }
}
}

import flash.events.Event;

import flash.events.IEventDispatcher;
import flash.utils.setTimeout;

import org.mockito.api.Answer;

class EventAnswer implements Answer {

    private var _dispatcher: IEventDispatcher;
    private var _eventToDispatch: Event;

    public function EventAnswer(dispatcher: IEventDispatcher, eventToDispatch: Event) {
        _dispatcher = dispatcher
        _eventToDispatch = eventToDispatch;
    }

    public function give():* {
        setTimeout(function(): void {
            _dispatcher.dispatchEvent(_eventToDispatch);
        }, 20);
    }
}

